﻿using BCCommon;
using BCCommon.TMS.Driver;
using BCData.Common.Area;
using BCData.TMS.WorkingReport;
using BCDto.TMS.Driver;
using BCDto.TMS.WorkingReport;
using BCEntity.Common.EntityNameConst;
using BCEntity.TMS.WorkingReport;
using BCService.TMS.WorkingReport;
using BCService.Utils;
using System;
using System.Collections.Generic;

namespace BCService.MapperProfiles.TMS.WorkingReport
{
    public class WorkingReportService : IWorkingReportService
    {
        private readonly IWorkingReportData workingReportData;
        private readonly IProvinceData provinceData;
        private readonly ICityData cityData;
        private readonly ICountyData countyData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.WorkingReportEntity);

        public WorkingReportService(IWorkingReportData workingReportData, IProvinceData provinceData, ICityData cityData, ICountyData countyData, IRedisService redisService)
        {
            this.workingReportData = workingReportData;
            this.provinceData = provinceData;
            this.cityData = cityData;
            this.countyData = countyData;
            this.redisService = redisService;
        }

        public WorkingReportDto Add(DriverDto driver, WorkingReportRequestDto dto)
        {
            var entity = dto.As<WorkingReportEntity>();
            if (dto.ProvinceId <= 0)
            {
                throw new ArgumentException("所在位置省份信息不能为空");
            }
            if (dto.CityId <= 0)
            {
                throw new ArgumentException("所在位置城市信息不能为空");
            }
            var province = this.provinceData.GetProvince(dto.ProvinceId);
            if (province == null)
            {
                throw new ArgumentException("省份信息不存在");
            }
            var city = this.cityData.GetCity(dto.CityId);
            if (city == null)
            {
                throw new ArgumentException("城市信息不存在");
            }
            if (dto.CountyId.HasValue)
            {
                var county = this.countyData.GetCounty(dto.CountyId.Value);
                if (county == null)
                {
                    throw new ArgumentException("区/县信息不存在");
                }
                entity.CountyCode = county.CountyCode;
                entity.CountyName = county.CountyName;
            }
            entity.ProvinceCode = province.ProvinceCode;
            entity.ProvinceName = province.ProvinceName;
            entity.CityCode = city.CityCode;
            entity.CityName = city.CityName;
            entity.Phone = driver.Phone;
            entity.DriverId = driver.DriverId;
            entity.Name = driver.Name;
            entity.Role = driver.Role.ToString();
            entity.CompanyId = driver.CompanyId.Value;

            var result = this.workingReportData.InsertAsync(entity).GetAwaiter().GetResult();
            if (result == null)
            {
                throw new ArgumentException("工作上报失败");
            }
            return result.As<WorkingReportDto>();
        }

        public WorkingReportDto GetByWorkingReportId(long workingReportId)
        {
            string cacheKey = this.redisService.GetKey(key, workingReportId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.workingReportData.GetAsync(workingReportId).GetAwaiter().GetResult().As<WorkingReportDto>();

                if (data == null)
                {
                    throw new ArgumentException("工作日报信息不存在。");
                }
                return data.As<WorkingReportDto>();
            }, TimeSpan.FromDays(365));
            return result;
        }



        public PageableList<WorkingReportDto> GetList(long? companyId, string phone, DateTime? start, DateTime? end, DriverRoleType? role, bool? ascending, string sortName, int pageIndex, int pageSize)
        {
            var queryResult = this.workingReportData.GetListAsync(companyId, phone, start, end, role, ascending, sortName, pageIndex, pageSize).GetAwaiter().GetResult();
            return new PageableList<WorkingReportDto>
            {
                Count = queryResult.Item2,
                Items = queryResult.Item1.As<IEnumerable<WorkingReportDto>>(),
                ServerTime = DateTime.Now,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
